/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import com.alibaba.fastjson.JSONObject;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.HljgService;
import me.zhengjie.business.service.SbgsService;
import me.zhengjie.business.service.dto.HljgDto;
import me.zhengjie.business.service.dto.HljgQueryCriteria;
import me.zhengjie.business.service.dto.SbgsDto;
import me.zhengjie.business.service.dto.SbgsSmallDto;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-04-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "护理机构管理")
@RequestMapping("/api/hljg")
public class HljgController {

    private final HljgService hljgService;
    private final SbgsService sbgsService;
    private final GsglService gsglService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('hljg:list')")
    public void exportHljg(HttpServletResponse response, HljgQueryCriteria criteria) throws IOException {
        hljgService.download(hljgService.queryAll(criteria), response);
    }

    @GetMapping(value = "/queryAllHljg")
    @ApiOperation("查询所有护理机构")
    public ResponseEntity<Object> queryAllHljg(HljgQueryCriteria criteria){
        return new ResponseEntity<>(hljgService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping(value = "/queryThisHljg")
    @ApiOperation("查询本护理机构")
    public ResponseEntity<Object> queryThisHljg(){
        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        HljgDto hljgDto = hljgService.findById(gsgl.getBisGsglId());
        JSONObject obj = new JSONObject();
        obj.put("sbIsExist", hljgDto.getSbgs() != null);
        obj.put("pgIsExist", hljgDto.getPggs() != null);
        obj.put("sstcq", hljgDto.getBisGsglYbqh());
        return new ResponseEntity<>(obj, HttpStatus.OK);
    }

    @GetMapping
    @ApiOperation("查询护理机构")
    @PreAuthorize("@el.check('hljg:list')")
    public ResponseEntity<Object> queryHljg(HljgQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(hljgService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/cihl")
    @Log("商保公司查询护理机构")
    @ApiOperation("商保公司查询护理机构")
    @PreAuthorize("@el.check('hljg:list')")
    public ResponseEntity<Object> querySbHljg(HljgQueryCriteria criteria, Pageable pageable){
        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisGsglId(gsgl.getBisGsglId());
        return new ResponseEntity<>(hljgService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/todo")
    @Log("商保公司查询护理机构")
    @ApiOperation("商保公司查询护理机构")
    public ResponseEntity<Object> queryTodo(@RequestParam(defaultValue = "") String name,
                                            @RequestParam(defaultValue = "0")Integer page,
                                            @RequestParam(defaultValue = "10")Integer size) {
        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(hljgService.queryTodo(name, gsgl.getBisGsglId(), startEnd),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增护理机构")
    @ApiOperation("新增护理机构")
    @PreAuthorize("@el.check('hljg:add')")
    public ResponseEntity<Object> createHljg(@Validated @RequestBody Hljg resources){
        return new ResponseEntity<>(hljgService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改护理机构")
    @ApiOperation("修改护理机构")
    @PreAuthorize("@el.check('hljg:edit')")
    public ResponseEntity<Object> updateHljg(@Validated @RequestBody Hljg resources){
        hljgService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除护理机构")
    @ApiOperation("删除护理机构")
    @PreAuthorize("@el.check('hljg:del')")
    public ResponseEntity<Object> deleteHljg(@RequestBody String[] ids) {
        hljgService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}